#!/usr/bin/env python
# -*- coding: utf-8 -*-
import datetime,dateutil.parser
from marketData_py2 import getHtml as getHtml_SH
from marketDataSZ_py2 import getHtml as getHtml_SZ
from database import DataBase

def main():
    dataBase = DataBase()
    date_now = datetime.datetime.now().strftime('%Y-%m-%d')
    # print date_now
    num =0 
    while num < 15:
        date_tmp = dateutil.parser.parse(date_now) + datetime.timedelta(-num)
        date_str = date_tmp.strftime('%Y-%m-%d')
        run(date_str, dataBase)
        num = num + 1
    # dataBase.close()

def run(date, dataBase=None):
    gp_line_num = 2
    gp_key = 'trdAmt1'
    jj_line_num = 1
    jj_key = 'trdAmt1'
    html_gp = "http://query.sse.com.cn/marketdata/tradedata/queryTradingByProdTypeData.do?jsonCallBack=jsonpCallback70306&searchDate="+date+"&prodType=gp&_=1485238356920"
    html_jj = "http://query.sse.com.cn/marketdata/tradedata/queryTradingByProdTypeData.do?jsonCallBack=jsonpCallback359&searchDate="+date+"&prodType=jj&_=1486348626560"
    data_gp = getHtml_SH(html_gp, gp_line_num, gp_key)
    data_jj = getHtml_SH(html_jj, jj_line_num, jj_key)
    (data_sz_gp, data_sz_jj) = getHtml_SZ("http://www.szse.cn/szseWeb/FrontController.szse?randnum=0.8567524743959484", date)
    if (data_gp is None) or (data_jj is None) or (data_sz_gp is None) or (data_sz_jj is None):
        pass
    else:
        # print(date, data_gp, data_jj, data_sz)
        # print(date, float(data_gp)*100000000, float(data_jj)*100000000)
        print(date, float(data_gp), float(data_jj) ,float(data_sz_gp)/100000000, float(data_sz_jj)/100000000)
        total_data = (float(data_gp) + float(data_jj) + float(data_sz_gp+data_sz_jj)/100000000) * 2
        print(date, total_data)
        sql1 = """
        merge into T_MARKET a
        using (select %s jzrq,%d*100000000 ha_stock,%d*100000000 ha_found,%f sz_stock,%f sz_found from dual) b
        on (a.jzrq = b.jzrq)
        when matched then
        update set ha_stock=b.ha_stock,ha_found=b.ha_found,sz_stock=b.sz_stock,sz_found=b.sz_found,update_time=sysdate
        when not matched then
        insert (jzrq,ha_stock,ha_found,sz_stock,sz_found,update_time,create_time) values( b.jzrq,b.ha_stock,b.ha_found,b.sz_stock,b.sz_found,sysdate,sysdate)
        """ % (date.replace('-', ''), float(data_gp), float(data_jj), int(data_sz_gp), int(data_sz_jj))
        sql2 = """
        update T_MARKET set total_value = (ha_stock+ha_found+sz_stock+sz_found)*2 where jzrq='%s'
        """ % (date.replace('-', '') ,)
        if dataBase is None:
            f_obj = open('D:\market_value.csv', 'a+')
            f_obj.write('%s,%f \n' % (date, total_data))
            f_obj.close()
            f_obj = open('D:\market_value_oralce_sql.csv', 'a+')
            f_obj.write(sql1+'; \n')
            f_obj.write(sql2 + '; \n')
            f_obj.close()
        else:
            dataBase.runSql(sql1)
            dataBase.session.commit()
            dataBase.runSql(sql2)
            dataBase.session.commit()

def init_database():
    from get_market_value.database import DataBase
    from sqlalchemy import create_engine
    from get_market_value.modules import Base
    database = DataBase()
    engine = create_engine(database.database_url, echo=True)
    Base.metadata.create_all(engine)


if __name__ == "__main__":
    main()